﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;

namespace YArchitech.HVAC
{
	public class PipeSet : List<HyCalcPipe>
	{
		public string Name { get; set; }

		public IList<Pipe> Pipes
		{
			get
			{
				return this.GetAllPipes();
			}
		}

		public IList<Element> Valves
		{
			get
			{
				return this.GetAllValveAndFittings();
			}
		}

		public IList<Element> Elements
		{
			get
			{
				return this.GetAllElements();
			}
		}

		private IList<Pipe> GetAllPipes()
		{
			this.pipes = new List<Pipe>();
			List<IList<PipeLine>> list = (from pipe in this
			select pipe.PipesLines).ToList<IList<PipeLine>>();
			List<PipeLine> list2 = new List<PipeLine>();
			foreach (IList<PipeLine> collection in list)
			{
				list2.AddRange(collection);
			}
			this.pipes = (from pl in list2
			select pl.Element).ToList<Pipe>();
			return this.pipes;
		}

		private IList<Element> GetAllValveAndFittings()
		{
			this.valves = new List<Element>();
			List<IList<ValveAndFitting>> list = (from pipe in this
			select pipe.Valves).ToList<IList<ValveAndFitting>>();
			List<ValveAndFitting> list2 = new List<ValveAndFitting>();
			foreach (IList<ValveAndFitting> collection in list)
			{
				list2.AddRange(collection);
			}
			this.valves = (from v in list2
			select v.Element).ToList<Element>();
			return this.valves;
		}

		private IList<Element> GetAllElements()
		{
			this.elements = new List<Element>();
			foreach (Pipe item in this.GetAllPipes())
			{
				this.elements.Add(item);
			}
			foreach (Element item2 in this.GetAllValveAndFittings())
			{
				this.elements.Add(item2);
			}
			return this.elements;
		}

		public void Add(PipeSet pSet)
		{
			base.AddRange(from ps in pSet
			select ps);
		}

		public HyCalcPipe At(int index)
		{
			return (from hypipe in this
			where hypipe.Index == index
			select hypipe).First<HyCalcPipe>();
		}

		public double SumResistance(int endIndex)
		{
			return (from p in this.AsParallel<HyCalcPipe>()
			where p.Index <= endIndex
			select p.Resistance).Sum();
		}

		public double SumResistance()
		{
			return (from p in this.AsParallel<HyCalcPipe>()
			select p.Resistance).Sum();
		}

		public void UpDate()
		{
			base.ForEach(delegate(HyCalcPipe pl)
			{
				pl.GetAllParam();
			});
		}

		public void BackRevit()
		{
			base.ForEach(delegate(HyCalcPipe pl)
			{
				pl.BackRevit();
			});
		}

		private IList<Pipe> pipes;

		private IList<Element> valves;

		private IList<Element> elements;
	}
}
